/**
 * 初始化详情对话框
 */
var InsuranceKindInfoDlg = {
    insuranceKindInfoData : {},
    canSubmit : true,
    validateFields: {
        insuranceKindCode: {
            validators: {
                notEmpty: {
                    message: '险种代码不能为空'
                }
            }
        },
        insuranceKindName: {
            validators: {
                notEmpty: {
                    message: '险种名称不能为空'
                }
            }
        },
        insuranceKindType: {
            validators: {
                notEmpty: {
                    message: '请选择险种类型'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
InsuranceKindInfoDlg.clearData = function() {
    this.insuranceKindInfoData = {};
}
/**
 * 选择计费保额方式
 */
InsuranceKindInfoDlg.selectAmountType = function(status) {
    if(!status){return;}
    //初始化计费方式
    $("#fixAmountLimit").val("");
    $("#fixAmount").val("");
    $("#floatAmountLimit").val("");
    $("#floatAmount").val("");
    $("#fixAmountLimit").removeAttr("disabled");
    $("#fixAmount").removeAttr("disabled");
    $("#floatAmountLimit").removeAttr("disabled");
    $("#floatAmount").removeAttr("disabled");
    $("input[name=amountType][value="+ status +"]").attr("checked",true);
    if(status == 1){
        $("#floatAmountLimit").attr("disabled","true");
        $("#floatAmount").attr("disabled","true");
    }else if(status == 2){
        $("#fixAmountLimit").attr("disabled","true");
        $("#fixAmount").attr("disabled","true");
    }
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
InsuranceKindInfoDlg.set = function(key, val) {
    this.insuranceKindInfoData[key] = (typeof val == "undefined") ? $("#" + key).val() : val;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
InsuranceKindInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
InsuranceKindInfoDlg.close = function() {
    parent.layer.close(window.parent.InsuranceKind.layerIndex);
}

/**
 * 收集数据
 */
InsuranceKindInfoDlg.collectData = function() {
    this
    .set('id')
    .set('policyId')
    .set('insuranceKindCode')
    .set('insuranceKindName')
    .set('insuranceKindType')
    .set('amountType',$("input[name='amountType']:checked").val())
    .set('fixAmountLimit')
    .set('fixAmount')
    .set('floatAmountLimit')
    .set('floatAmount')
    .set('insuranceComment')
    .set('insuranceExplain');
    if($("input[name='amountType']:checked").val() == 1){
        this
        .set('amountLimit',this.get('fixAmountLimit'))
        .set('amount',this.get('fixAmount'))
    }else{
        this
        .set('amountLimit',this.get('floatAmountLimit'))
        .set('amount',this.get('floatAmount'))
    }
}

/**
 * 验证数据
 */
InsuranceKindInfoDlg.validate = function () {
    $('#insuranceKindInfoForm').data("bootstrapValidator").resetForm();
    $('#insuranceKindInfoForm').bootstrapValidator('validate');
    var result = $("#insuranceKindInfoForm").data('bootstrapValidator').isValid();
    if(result){
        var amountType = $("input[name='amountType']:checked").val();
        //固定保额
        if(amountType==1){
            var fixAmountLimit = this.get("fixAmountLimit");
            var fixAmount = this.get("fixAmount");
            if(!fixAmountLimit || fixAmountLimit<=0){
                Feng.alert("保额需要填写正数的数值!");
                return false;
            }
            if(!fixAmount || fixAmount<=0){
                Feng.alert("保费需要填写正数的数值!");
                return false;
            }
        }else if(amountType==2){
            var floatAmountLimit = this.get("floatAmountLimit");
            var floatAmount = this.get("floatAmount");
            if(!floatAmountLimit || floatAmountLimit<=0){
                Feng.alert("免核保额度需要填写正数的数值!");
                return false;
            }
            if(floatAmount>100 || floatAmount<0 || floatAmount==""){
                Feng.alert("保费比例需要填写0-100的数值!");
                return false;
            }
        }else{
            Feng.alert("请选择保额及保费!");
            return false;
        }
    }
    return result;
};
/**
 * 提交添加
 */
InsuranceKindInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    if (!this.canSubmit) {
        return;
    }
    this.canSubmit = false;
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/insuranceKind/add", function(data){
        Feng.success("添加成功!");
        window.parent.InsuranceKind.table.refresh();
        InsuranceKindInfoDlg.canSubmit = true;
        InsuranceKindInfoDlg.close();
    },function(data){
        setTimeout(function () {
            InsuranceKindInfoDlg.canSubmit = true;
        },3000);
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.insuranceKindInfoData);
    ajax.start();
}

/**
 * 提交修改
 */
InsuranceKindInfoDlg.editSubmit = function() {

    this.clearData();
    this.collectData();

    if (!this.validate()) {
        return;
    }
    if (!this.canSubmit) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/insuranceKind/update", function(data){
        Feng.success("修改成功!");
        window.parent.InsuranceKind.table.refresh();
        InsuranceKindInfoDlg.canSubmit = true;
        InsuranceKindInfoDlg.close();
    },function(data){
        setTimeout(function () {
            InsuranceKindInfoDlg.canSubmit = true;
        },3000);
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.insuranceKindInfoData);
    ajax.start();
}

$(function() {
    Feng.initValidator("insuranceKindInfoForm", InsuranceKindInfoDlg.validateFields);
    //险种类型
    $("#insuranceKindType").val($("#insuranceKindTypeValue").val());
    //保额类型
    InsuranceKindInfoDlg.selectAmountType($("#amountTypeValue").val());
    if($("#amountTypeValue").val() == 1){
        $("#fixAmountLimit").val($("#amountLimitValue").val());
        $("#fixAmount").val($("#amountValue").val());
    }else if($("#amountTypeValue").val() == 2){
        $("#floatAmountLimit").val($("#amountLimitValue").val());
        $("#floatAmount").val($("#amountValue").val());
    }
    //readOnly
    if($("#status").val() == 2){
        $("#insuranceKindCode").attr('disabled','true');
        $("#insuranceKindName").attr('disabled','true');
        $("#insuranceKindType").attr('disabled','true');
        $("input:radio").attr('disabled','true');
        $("#fixAmountLimit").attr('disabled','true');
        $("#fixAmount").attr('disabled','true');
        $("#floatAmountLimit").attr('disabled','true');
        $("#floatAmount").attr('disabled','true');
        $("#insuranceExplain").attr('disabled','true');
        $("#insuranceComment").attr('disabled','true');
    }
});
